//
//  PhotoBrowserAnimator.swift
//  SwiftPhophotoBrowser
//
//  Created by xmg on 16/8/20.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit
/** 设置代理 */
protocol AnimatorPresentedDelegate : class {
    func startRect(indexPath : NSIndexPath) -> CGRect
    func imageView(indexPath : NSIndexPath) -> UIImageView
    func endRect(indexPath : NSIndexPath) -> CGRect
}

/** 设置代理 */
protocol AnimatorDismissedDelegate : class {
    func getCurrentIndexPath() -> NSIndexPath
    func getCurrentImageView() -> UIImageView
}

class PhotoBrowserAnimator: NSObject {

    /** 定义属性 */
    private var isPresented :Bool = true;
    var indexPath : NSIndexPath?
    /** 设置代理属性 */
    weak var presentedDelegate : AnimatorPresentedDelegate?
    weak var dismissedDelegate : AnimatorDismissedDelegate?
}

extension PhotoBrowserAnimator : UIViewControllerTransitioningDelegate{

    /** 弹出控制器做动画 */
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
}
extension PhotoBrowserAnimator : UIViewControllerAnimatedTransitioning{

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 2.0
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        if isPresented{
        
            /** 判断代理是否有值 */
            guard let presentedDelegate = presentedDelegate else{return}
            
            guard let indexPath = indexPath else {return}
            /** 获取弹出的View */
            let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)
            transitionContext.containerView()?.addSubview(presentedView!)
            /** 设置影藏 */
            presentedView?.alpha = 0.0
            /** 设置背景颜色 */
            transitionContext.containerView()?.backgroundColor = UIColor.blackColor()
            /** 从代理设置获取图片 */
            let imageView : UIImageView = presentedDelegate.imageView(indexPath)
            transitionContext.containerView()?.addSubview(imageView)
            /** 从代理设置开始位置 */
            let startRect : CGRect = presentedDelegate.startRect(indexPath)
            imageView.frame = startRect
            
            UIView.animateWithDuration(2.0, animations: { () -> Void in
               
                let endRect : CGRect = presentedDelegate.endRect(indexPath)
                imageView.frame = endRect
                
                }, completion: { (isFinished:Bool) -> Void in
                   imageView.removeFromSuperview()
                    presentedView?.alpha = 1.0
                    transitionContext.containerView()?.backgroundColor = UIColor.clearColor()
                    transitionContext.completeTransition(isFinished)
            })
            
        }else{
        
            guard let dismissedDelegate = dismissedDelegate else {return}
            guard let presentedDelegate = presentedDelegate else {return}
            
            let dismissedView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            dismissedView?.removeFromSuperview()
            
            let indexPath = dismissedDelegate.getCurrentIndexPath()
            let imageView = dismissedDelegate.getCurrentImageView()
            
            transitionContext.containerView()?.addSubview(imageView)
            
            UIView.animateWithDuration(2.0, animations: { () -> Void in
                let endRect = presentedDelegate.startRect(indexPath)
                if endRect == CGRectZero {
                    imageView.alpha = 0.0;
                }else {
                    imageView.frame = endRect;
                }
                
                }, completion: { (isFinished:Bool) -> Void in
                    transitionContext.completeTransition(isFinished)
            })
        }
    }

}
